package com.lianxi.Courses.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.CommentConstant;
import com.lianxi.Courses.constant.DictConstant;
import com.lianxi.Courses.constant.StatusConstant;
import com.lianxi.Courses.dao.CommentMapper;
import com.lianxi.Courses.dao.PraiseMapper;
import com.lianxi.Courses.entity.*;
import com.lianxi.Courses.exception.PraiseErrorException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.CommentService;
import com.lianxi.Courses.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 翔哥
 * 2025年10月13日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PraiseMapper praiseMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Comment> findPage(Comment commentParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COMMENT_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COMMENT_DEFAULT_PAGE_SIZE;
        }

        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<Comment>  comments = commentMapper.selectAll(commentParam);

        for (Comment comment :comments){
            PraiseExample example =new PraiseExample();
            example.or()
                    .andCommentIdEqualTo(comment.getId());
            List<Praise> praises = praiseMapper.selectByExample(example);
            comment.setLike(praises.size());
        }


        PageInfo<Comment> commentPage = new PageInfo<>(comments);
        return commentPage;
    }

    @Override
    public void enableById(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment =commentMapper.selectByPrimaryKey(Integer.valueOf(id));
        comment.setStatus(CommentConstant.COMMENT_ENABLE);
        commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Override
    public void disableById(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment =commentMapper.selectByPrimaryKey(Integer.valueOf(id));
        comment.setStatus(CommentConstant.COMMENT_DISABLE);
        commentMapper.updateByPrimaryKeySelective(comment);
    }

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Override
public PageInfo<Comment> findByResourceId(String resourceId, String pageNo, String pageSize) throws RequestParameterErrorException {
    if (ParameterUtil.isNull(resourceId)){
        throw new RequestParameterErrorException("请求参数有误");
    }
    if (ParameterUtil.isNull(pageNo)) {
        pageNo = DictConstant.COMMENT_DEFAULT_PAGE_NO;
    }
    if (ParameterUtil.isNull(pageSize)) {
        pageSize = DictConstant.COMMENT_DEFAULT_PAGE_SIZE;
    }

    PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
    CommentExample example =new CommentExample();
    example.or()
            .andResourceIdEqualTo(Integer.valueOf(resourceId));
    List<Comment> comments = commentMapper.selectByExample(example);

    for (Comment comment :comments){
        PraiseExample example1 =new PraiseExample();
        example1.or()
                .andCommentIdEqualTo(comment.getId());
        List<Praise> praises = praiseMapper.selectByExample(example1);
        comment.setLike(praises.size());
    }

    PageInfo<Comment> commentPage = new PageInfo<>(comments);
    return commentPage;
}


    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setStatus(Integer.parseInt(status));
        if (comment.getStatus() == CommentConstant.COMMENT_ENABLE) {
            comment.setStatus(CommentConstant.COMMENT_DISABLE);
        } else {
            comment.setStatus(CommentConstant.COMMENT_ENABLE);
        }
        commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Override
    public void insertComment(String userId,String context,String resourceId) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(context)|| ParameterUtil.isNull(userId) ||ParameterUtil.isNull(resourceId)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment=new Comment();
        comment.setContext(context);
        comment.setUserId(Integer.valueOf(userId));
        comment.setResourceId(Integer.valueOf(resourceId));
        comment.setCreateDate(new Date());
        comment.setStatus(CommentConstant.COMMENT_STATE);

        commentMapper.insertSelective(comment);
    }

    @Override
    public void addLike(String userId, String commentId) throws RequestParameterErrorException, PraiseErrorException {
        if (ParameterUtil.isNull(userId)||ParameterUtil.isNull(commentId)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        PraiseExample  praiseExample =new PraiseExample();
        praiseExample.or()
                .andUserIdEqualTo(Integer.valueOf(userId))
                .andCommentIdEqualTo(Integer.valueOf(commentId));

        List<Praise> praises =praiseMapper.selectByExample(praiseExample);
        if (!praises.isEmpty()){
            praiseMapper.deleteByExample(praiseExample);
        }else {
            Praise praise =new Praise();
            praise.setUserId(Integer.valueOf(userId));
            praise.setCommentId(Integer.valueOf(commentId));
            praise.setCreateDate(new Date());
            praiseMapper.insertSelective(praise);
        }
    }

    @Override
    public Praise findPraise(String userId, String commentId) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(userId)||ParameterUtil.isNull(commentId)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        PraiseExample praiseExample =new PraiseExample();
        praiseExample.or()
                .andUserIdEqualTo(Integer.valueOf(userId))
                .andCommentIdEqualTo(Integer.valueOf(commentId));
        List<Praise> praises = praiseMapper.selectByExample(praiseExample);
        if (praises.isEmpty()) {
            return null;
        } else {
            return praises.get(0);
        }

    }


}
